﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using WBGModel;
using System.Data;

namespace WBGDal
{
    public class DalWebNews : ICRUD
    {
        public List<ModelWebNews> Listar()
        {
            return Listar("");
        }

        public List<ModelWebNews> Listar(string filtro)
        {
            String cmd = "SELECT * FROM [WBG].[WebNews]" + (filtro.CompareTo("") == 0 ? "" : " WHERE " + filtro);
            return ConvertToList(UtilBanco.ExecComRetorno(cmd));
        }

        private List<ModelWebNews> ConvertToList(DataTable resultSet)
        {
            List<ModelWebNews> lst = new List<ModelWebNews>();
            for (int i = 0; i < resultSet.Rows.Count; i++)
            {
                DataRow dr = resultSet.Rows[i];
                ModelWebNews noticia = new ModelWebNews();
                noticia.idNews = Convert.ToInt32(dr["idNews"]);
                noticia.visible = Convert.ToBoolean(dr["visible"]);
                noticia.creationDate = Convert.ToDateTime(dr["creationDate"]);
                noticia.title = dr["title"].ToString();
                noticia.bodyNews = dr["bodyNews"].ToString();
                noticia.priorityNews = Convert.ToInt32(dr["priorityNews"]);
                lst.Add(noticia);
            }
            return lst;
        }
        public void Inserir(ModelWebNews obj)
        {
            String cmd = String.Format("INSERT INTO [WBG].[WebNews] (title,bodyNews,visible,creationDate,priorityNews) VALUES ('{0}','{1}','{2}','{3}',{4})", obj.title, obj.bodyNews, obj.visible, obj.creationDate, obj.priorityNews);
            UtilBanco.ExecSemRetorno(cmd);
        }

        public void Alterar(ModelWebNews obj)
        {
            String cmd = String.Format("UPDATE [WBG].[WebNews] SET title='{0}',bodyNews='{1}',visible='{2}',creationDate='{3}',priorityNews={4} WHERE idNews={5}", obj.title, obj.bodyNews, obj.visible, obj.creationDate, obj.priorityNews, obj.idNews);
            UtilBanco.ExecSemRetorno(cmd);
        }

        public void Deletar(int id)
        {
            String cmd = String.Format("DELETE FROM [WBG].[WebNews] WHERE idNews={0}", id);
            UtilBanco.ExecSemRetorno(cmd);
        }
    }
}
